package com.haojin.spring.resources;

import org.springframework.core.io.FileSystemResource;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author .29.
 * @create 2023-03-02 20:32
 */
public class FileSystemResourceDemo {
    public static void main(String[] args){
        loadFileSystemResource("d:\\haojin.txt"); //path是绝对路径
    }
    public static void loadFileSystemResource(String path){
        FileSystemResource fileSystemResource = new FileSystemResource(path);
        //获取系统资源的信息
        System.out.println(fileSystemResource.getFilename());
        System.out.println(fileSystemResource.getDescription());

        //获取资源内容
        try {
            InputStream inputStream = fileSystemResource.getInputStream();
            byte[] bytes = new byte[1024];
            if(inputStream.read(bytes) != 1){
                System.out.println(new String(bytes));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
